﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web;
using TRPOP.Common;
using Newtonsoft.Json;
using System.Data;
using TRPOP.CMS.Message.model;

// 获取 留言+回复 信息，返回json格式的数据
/*
 * 
 *   如果传递 jsoncallback ，则说明是jquery的$.gejson调用的，那么此时：
 *       访问此页面的数据js是可以跨域访问的(js编程会有跨域的限制)
 *   如果没有传递，则正常的输出JSON数据,此时如果通过js访问数据，是有跨域限制的
 * 
 */
/*
 * 
 *   传递的数据:
 *      pagesize ：     每页数据大小 默认10
 *      indexpage ：    当前页次（从1开始） 默认1
 *      MessageStatus： 留言信息状态限制 默认2
 *      OrderBy：       排序  默认：[iMessageID] DESC
 *      
 *
 *   返回的数据  JSON数据格式 例如数据是 Data ,则：
 * 　　　Data.PageSize
 * 　　　Data.IndexPage
 * 　　　Data.RecordCount
 * 　　　Data.DataList[*].MessageID
 * 　　　Data.DataList[*].MessageTitle
 * 　　　Data.DataList[*].MessageCreationTime
 * 　　　Data.DataList[*].MessageContent
 * 　　　
 * 　　　Data.DataList[*].ReplyTitle
 * 　　　Data.DataList[*].ReplyContent
 * 　　　Data.DataList[*].ReplyUserName
 * 　　　Data.DataList[*].ReplyTime
 * 　　　Data.DataList[*].ReplyID
 * 　　　
 * 　　　Data.DataList[*].ContactName
 * 　　　Data.DataList[*].ContactGender
 * 　　　Data.DataList[*].ContactAddress
 * 　　　Data.DataList[*].ContactEmail
 * 　　　Data.DataList[*].ContactMobile
 * 　　　Data.DataList[*].ContactPhone
 * 　　　Data.DataList[*].ContactPostCode
 * 　　　
 */

namespace TRPOP.CMS.Message.UI
{
    /// <summary>
    /// 获取分页的 留言+回复 数据
    /// </summary>
    public class GetMessage : IHttpHandler
    {
        protected IMessage m_IMessage = MessageAccess.GetInstance();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (RequestHelper.RequestString("jsoncallback").IsEmpty() == false)//jquery 支持跨域访问数据
            {
                context.Response.Write(RequestHelper.RequestString("jsoncallback") + "(" + GetJSON(context) + ")");
                context.Response.End();
            }
            else
            {
                context.Response.Write(GetJSON(context));
                context.Response.End();
            }

        }

        public string GetJSON(HttpContext context)
        {

            JavaScriptObject oJSONobject = new JavaScriptObject();

            int intPageSize = 10;
            intPageSize = RequestHelper.RequestInt("pagesize");
            if (intPageSize <= 0)
            {
                intPageSize = 10;
            }

            int intIndexPage = RequestHelper.RequestInt("indexpage");
            if (intIndexPage <= 0)
                intIndexPage = 1;

            int intMessageStatus = RequestHelper.RequestInt("MessageStatus");
            if (intMessageStatus <= 0)
                intMessageStatus = 2;

            string strCondition = " AND bIsDelete='false' AND iMessageReplyID >0 AND  [iMessageStatusID]=" + intMessageStatus;

            //留言主题搜索
            if (!RequestHelper.RequestString("title").IsEmpty())
            {
                strCondition += " AND  [nvcMessageTitle] like '%" + RequestHelper.RequestString("title").SQLFilter() + "%'  ";
            }

            //留言内容
            if (!RequestHelper.RequestString("content").IsEmpty())
            {
                strCondition += " AND  [ntMessageContent] like '%" + RequestHelper.RequestString("content").SQLFilter() + "%' ";
            }
            
            //留言对象id
            if (RequestHelper.RequestInt("iSend") > 0)
            {
                strCondition += " AND  [iMessageSend] = " + RequestHelper.RequestInt("iSend") + " ";
            }

            //留言对象str
            if (!RequestHelper.RequestString("nvcSend").IsEmpty())
            {
                strCondition += " AND  [nvcMessageSend] = '" + RequestHelper.RequestString("nvcSend").SQLFilter() + "' ";
            }

            //留言类型
            if (RequestHelper.RequestInt("type") > 0)
            {
                strCondition += " AND  [iMessageTypeID] = " + RequestHelper.RequestInt("type") + " ";
            }
            else if (RequestHelper.RequestString("type").IsEmpty() == false)
            {
                strCondition += " AND  [iMessageTypeID] in (" + RequestHelper.RequestString("type") + ") ";
            }

            //回复人
            if (!RequestHelper.RequestString("ReplyUserName").IsEmpty())
            {
                strCondition += " AND  [nvcMessageReplyUserName] = '" + RequestHelper.RequestString("ReplyUserName").SQLFilter() + "' ";
            }

            //回复内容
            if (!RequestHelper.RequestString("ReplyContent").IsEmpty())
            {
                strCondition += " AND  [ntMessageReplyContent] like '%" + RequestHelper.RequestString("ReplyContent").SQLFilter() + "%' ";
            }




            string strOrderBy = RequestHelper.RequestString("OrderBy");
            if (strOrderBy.IsEmpty())
                strOrderBy = " [iMessageID] DESC ";

            int intRecordCount = 0;

            List<Message_MessageReplyInfo> oMessage_MessageReplyInfoList = m_IMessage.GetMessage_MessageReplyInfoListByPager(intIndexPage, intPageSize, strCondition, strOrderBy, out intRecordCount);

            oJSONobject.Add("PageSize", intPageSize);
            oJSONobject.Add("IndexPage", intIndexPage);
            oJSONobject.Add("RecordCount", intRecordCount);

            JavaScriptObject oDataListObject = new JavaScriptObject();

            foreach (Message_MessageReplyInfo oInfo in oMessage_MessageReplyInfoList)
            {
                JavaScriptObject oJavaScriptObjectTemp = new JavaScriptObject();

                oJavaScriptObjectTemp.Add("MessageID", oInfo.iMessageID);
                oJavaScriptObjectTemp.Add("MessageTitle", oInfo.nvcMessageTitle);
                oJavaScriptObjectTemp.Add("MessageCreationTime", oInfo.dtCreationTime.ToString());
                oJavaScriptObjectTemp.Add("MessageContent", oInfo.ntMessageContent);

                oJavaScriptObjectTemp.Add("ReplyTitle", oInfo.nvcMessageReplyTitle);
                oJavaScriptObjectTemp.Add("ReplyContent", oInfo.ntMessageReplyContent);
                oJavaScriptObjectTemp.Add("ReplyUserName", oInfo.nvcMessageReplyUserName);
                oJavaScriptObjectTemp.Add("ReplyTime", oInfo.dtMessageReplyTime.ToString());
                oJavaScriptObjectTemp.Add("ReplyID", oInfo.iMessageReplyID);

                oJavaScriptObjectTemp.Add("ContactName", oInfo.nvcContactName);
                oJavaScriptObjectTemp.Add("ContactGender", oInfo.iContactGender);
                oJavaScriptObjectTemp.Add("ContactAddress", oInfo.nvcContactAddress);
                oJavaScriptObjectTemp.Add("ContactEmail", oInfo.nvcContactEmail);
                oJavaScriptObjectTemp.Add("ContactMobile", oInfo.nvcContactMobile);
                oJavaScriptObjectTemp.Add("ContactPhone", oInfo.nvcContactPhone);
                oJavaScriptObjectTemp.Add("ContactPostCode", oInfo.nvcContactPostCode);

                oDataListObject.Add(oInfo.iMessageID.ToString() + "," + oInfo.iMessageReplyID.ToString(), oJavaScriptObjectTemp);

            }

            oJSONobject.Add("DataList", oDataListObject);

            return JavaScriptConvert.SerializeObject(oJSONobject);

        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}
